package com.epalmpay.service.ys.memberRedActivityService.impl;

import com.epalmpay.entity.Member;
import com.epalmpay.entity.TradeOffline;
import com.epalmpay.entity.YsMemberRedActivity;
import com.epalmpay.entity.YsMemberRedActivityPerfect;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.mapper.MemberMapper;
import com.epalmpay.mapper.TradeOfflineMapper;
import com.epalmpay.mapper.YsMemberRedActivityMapper;
import com.epalmpay.mapper.YsMemberRedActivityPerfectMapper;
import com.epalmpay.service.ys.memberRedActivityService.IYsMemberRedActivityPerfectService;
import com.epalmpay.util.StringUtil;
import com.epalmpay.util.Tools;
import com.epalmpay.util.payplat.UtilFun;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by gwn on 2018/1/22.
 */
@Service("ysMemberRedActivityPerfectService")
public class YsMemberRedActivityPerfectServiceImpl implements IYsMemberRedActivityPerfectService{

    @Resource
    private YsMemberRedActivityPerfectMapper ysMemberRedActivityPerfectMapper;

    @Resource
    private YsMemberRedActivityMapper ysMemberRedActivityMapper;

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private TradeOfflineMapper  tradeOfflineMapper;

    @Override
    public int insertMemberRedActivityPerfectInfo(String memberId, String verityStatus) throws ApplicationException {
        int result = 0;

        if(StringUtil.isEmpty(memberId)){
            throw new ApplicationException("会员(memberId:" + memberId + ")不能为空!");
        }

        //查询进行中的活动
        YsMemberRedActivity ysMemberRedActivity =ysMemberRedActivityMapper.queryMemberRedActivity();

        Integer count = ysMemberRedActivity.getRebateTime();


        for(int i =0;i<count;i++){
            YsMemberRedActivityPerfect ysMemberRedActivityPerfect = new YsMemberRedActivityPerfect();
            Date date = new Date();
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.MONTH, i);
            date=calendar.getTime();
            System.out.print(date.toString());
            ysMemberRedActivityPerfect.setActivityId(ysMemberRedActivity.getId());
            ysMemberRedActivityPerfect.setGroupId(ysMemberRedActivity.getGroupId());
            ysMemberRedActivityPerfect.setGmtCreate(date);
            ysMemberRedActivityPerfect.setGmtModified(date);
            ysMemberRedActivityPerfect.setMemberId(Long.parseLong(memberId));
            ysMemberRedActivityPerfect.setVerityStatus(Integer.parseInt(verityStatus));
            String orderNo = UtilFun.createOrderNum();
            ysMemberRedActivityPerfect.setOrderNo(orderNo);
            ysMemberRedActivityPerfect.setIsRebate(0);
            ysMemberRedActivityPerfectMapper.insertSelective(ysMemberRedActivityPerfect);


            //生成返利订单交易记录
            TradeOffline tradeOffline = new TradeOffline();
            tradeOffline.setOrderId(orderNo);
            tradeOffline.setGmtCreate(new Date());
            tradeOffline.setGmtModified(new Date());
            tradeOffline.setGroupId(ysMemberRedActivity.getGroupId());
            tradeOffline.setTransactionStatus((byte) BizEnum.TransactionStatus.WaitingForPay.getType());

            tradeOfflineMapper.insertSelective(tradeOffline);

        }


        //查询完善资料会员的等级 更新到ysMemberRedActivity 表中
        Member member =memberMapper.selectByPrimaryKey(Long.parseLong(memberId));
        if(ysMemberRedActivity.getGradeIds()==null){
            ysMemberRedActivity.setGradeIds(member.getGradeId().toString());
        }else if(ysMemberRedActivity.getGradeIds().contains(member.getGradeId().toString())){
            ysMemberRedActivity.setGradeIds(ysMemberRedActivity.getGradeIds());
        }else{
            ysMemberRedActivity.setGradeIds(ysMemberRedActivity.getGradeIds()+','+member.getGradeId().toString());
        }
        ysMemberRedActivity.setGmtModified(new Date());

        ysMemberRedActivityMapper.updateByPrimaryKeySelective(ysMemberRedActivity);




        return result;
    }
}
